use axum::{
    body::Body,
    extract::{Path, Query, State},
    http::Response,
    response::IntoResponse,
};
use serde::{Deserialize, Serialize};
use sky_pojo::{entities::category::Model, vo::dish::DishItemVO};

use crate::{
    bootstrap::AppState,
    router::resp::RespVO,
    service::{self, category},
};

#[derive(Debug, Serialize, Deserialize)]
pub struct CategoryQuery {
    r#type: i32,
}

pub async fn list_by_type(
    State(state): State<AppState>,
    Query(query): Query<CategoryQuery>,
) -> Response<Body> {
    let result = category::list_by_type(state.db_pool, query.r#type).await;

    match result {
        Ok(dish_vec) => {
            RespVO::<Vec<Model>>::success("success".to_owned(), Some(dish_vec)).into_response()
        }
        Err(e) => RespVO::<()>::error_without_data(e).into_response(),
    }
}
